<template>
  <el-dialog v-model="visible" title="日志列表" :close-on-click-modal="false" :close-on-press-escape="false" width="75%">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.jobId" placeholder="任务ID" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      border
      @sort-change="dataListSortChangeHandle"
      height="460"
      style="width: 100%;">
      <el-table-column prop="jobId" label="任务ID" header-align="center" align="center" width="80"></el-table-column>
      <el-table-column prop="beanName" label="bean名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="params" label="参数" header-align="center" align="center"></el-table-column>
      <el-table-column prop="status" label="状态" header-align="center" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 1" size="small">成功</el-tag>
          <el-tag v-else type="danger" size="small" @click.native="showErrorInfo(scope.row.id)" style="cursor: pointer;">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="times" label="耗时(单位: 毫秒)" header-align="center" align="center"></el-table-column>
      <el-table-column prop="createDate" label="执行时间" header-align="center" align="center" width="180"></el-table-column>
    </el-table>
	  <c-pagination @page-change="pageChange" :total="total" :page="page" :limit="limit"></c-pagination>
  </el-dialog>
</template>

<script setup>
import {nextTick, reactive, ref, unref} from "vue";
import CPagination from "../../../components/c-pagination.vue";
import http from "../../../utils/request.js";
import {exportHandle, message, messageBox} from "../../../utils/commonScript.js";


const url = '/sys/scheduleLog'

const addOrUpdateRef = ref()
const addOrUpdateVisible = ref(false)
const page = ref(1)
const limit = ref(10)
const order = ref('') // 排序，asc／desc
const orderField = ref('') // 排序，字段
const dataForm = reactive({
	jobId: ''
})
const dataListSelections = ref([])
const dataListLoading = ref(false) // 数据列表，loading状态
const dataList = ref([]) // 数据列表
const total = ref(0) // 总条数

const visible = ref(false)

const init = () => {
	visible.value = true
	getDataList()
}
// 失败信息
const showErrorInfo = (id) => {
	http.get(`/sys/scheduleLog/${id}`).then(({ data: res }) => {
		if (res.code !== 0) {
			return message.error(res.msg)
		}
		messageBox.alert(res.data.error)
	}).catch(() => {})
}
const query = () => {
	dataListLoading.value = true
	http.get(`${url}/page`,
		{
			params: {
				order: unref(order),
				orderField: unref(orderField),
				page:  page.value,
				limit: limit.value,
				...dataForm
			}
		}
	).then(({data: res}) => {
		dataListLoading.value = false
		if (res.code !== 0) {
			dataList.value = []
			total.value = 0
			return message.error(res.msg)
		}
		dataList.value = res.data.list
		total.value = res.data.total
	}).catch(() => {
		dataListLoading.value = false
	})
}
query()
// 查询
const getDataList = () => {
	page.value = 1
	limit.value = 10
	query()
}
// 分页
const pageChange = (p, l) => {
	page.value = p
	limit.value = l
	query()
}

// 多选
const dataListSelectionChangeHandle = (val) => {
	dataListSelections.value = val
}
// 排序
const dataListSortChangeHandle = (data) => {
	if (!data.order || !data.prop) {
		order.value = ''
		orderField.value = ''
		return false
	}
	order.value = data.order.replace(/ending$/, '')
	orderField.value = data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
	query()
}
// 新增 / 修改
const addOrUpdateHandle = (id='') => {
	addOrUpdateVisible.value = true
	nextTick(() => {
		addOrUpdateRef.value.init(id)
	})
}
// 删除
const deleteHandle = (id) => {
	if (!id && dataListSelections.value.length <= 0) {
		return message({
			message: '请选择删除项',
			type: 'warning',
			duration: 500
		})
	}
	messageBox.confirm('确定进行删除操作?', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	}).then(() => {
		const params = {'data': id ? [id] : dataListSelections.value.map(item => item['id'])}
		http.delete(url, params).then(({data: res}) => {
			if (res.code !== 0) {
				return message.error(res.msg)
			}
			message({
				message: '操作成功',
				type: 'success',
				duration: 500,
				onClose: () => {
					query()
				}
			})
		}).catch(() => {
		})
	}).catch(() => {
	})
}
defineExpose({
	init
})
</script>
